﻿package com.jfdesgagne.jfdesgagne09.core.view {
	
	import flash.display.DisplayObject;
	import com.jfdesgagne.jfdesgagne09.JFDesgagne09;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import fl.lang.Locale;
	import flash.utils.getDefinitionByName;
	import flash.geom.Point;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import gs.*; 
	import gs.easing.*;

	
	public class TooltipView extends Sprite {
		private var _jfDesgagne:JFDesgagne09;
		private var _tooltip_mc:MovieClip;
		private var _drawning_bmp:Bitmap;
		private var _line_sp:Sprite;
		private var _timer:Timer;
		
		public function TooltipView(_jfDesgagne:JFDesgagne09) {
			this._jfDesgagne = _jfDesgagne;
			this._timer = new Timer(1000, 1);
			this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, this._timerCompleteHandler);
			this._initView();
		}
		
		private function _initView():void {
			this._tooltip_mc = new TooltipMc();
			this.addChild(this._tooltip_mc);
			this._line_sp = new Sprite();
			this._line_sp.graphics.beginFill(0xfff600, 1);
			this._line_sp.graphics.drawRect(0, 0, 10, 20);
			this._line_sp.graphics.endFill();
			this.addChild(this._line_sp);
			this._tooltip_mc.drawning_txt.text = Locale.loadString("IDS_TOOLTIP_DRAWNING");
			this._tooltip_mc.keyboard_txt.text = Locale.loadString("IDS_TOOLTIP_KEYBOARD");
		}
		
		private function _timerCompleteHandler(event:TimerEvent):void {
			this._show();
		}
		
		private function _show():void {
			this._timer.stop();
			TweenMax.to(this, 0, {blurFilter:{blurX:50, blurY:50}});
			TweenMax.to(this, 0.3, {blurFilter:{blurX:0, blurY:0}, autoAlpha:1});
			if(!this._jfDesgagne.contains(this as DisplayObject)) this._jfDesgagne.addChild(this);
		}
		
		public function show(_itemRollOver:DisplayObject, _letter:String, _text:String):void {
			this.alpha = 0;
			this._tooltip_mc.letter_txt.text = _letter.toUpperCase();
			this._tooltip_mc.info_txt.text = Locale.loadString(_text).toUpperCase();
			try { 
				var moduleClass:Object = getDefinitionByName("TooltipLetter"+_letter) as Class;
          		var m:Object = new moduleClass(0, 0); 
				this._drawning_bmp = new Bitmap(m as BitmapData);
				this._drawning_bmp.x = 20;
				this._drawning_bmp.y = 43;
				this._tooltip_mc.addChild(this._drawning_bmp);
			} catch (e:ArgumentError) {
				trace(e); // output: ArgumentError: Error #2024: An object may not be added as a child of itself.
			}
			var point:Point = new Point();
				point = _itemRollOver.localToGlobal(point);
			
			this._line_sp.x = point.x + (_itemRollOver.width / 2) - this._line_sp.width/2;
			this._line_sp.y = point.y + _itemRollOver.height - 1;
			this._tooltip_mc.x = this._line_sp.x + (this._line_sp.width/2) - this._tooltip_mc.width/2;
			this._tooltip_mc.y = this._line_sp.y + this._line_sp.height;
			
			this._timer.reset();
			this._timer.start();
		}
		
		public function hide():void {
			this._timer.stop();
			if(this._jfDesgagne.contains(this as DisplayObject)) this._jfDesgagne.removeChild(this);
			if(this._drawning_bmp) if(this._tooltip_mc.contains(this._drawning_bmp as DisplayObject)) this._tooltip_mc.removeChild(this._drawning_bmp as DisplayObject);			
			this._drawning_bmp = null;
		}
	}
}